package com.xieyong.leetcode;

import java.math.BigInteger;

public class Leetcode2311 {
    public static void main(String[] args) {
        Leetcode2311 leetcode2311 = new Leetcode2311();
        String s = "1001010";
        int k = 5;
        int i = leetcode2311.longestSubsequence(s, k);
        System.out.println(i);
    }


    public int longestSubsequence(String s, int k) {
        StringBuffer ans = new StringBuffer();
        for (int i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) == '0') {
                ans.insert(0, '0');
            } else {
                String temp = "1" + ans;
                BigInteger num = new BigInteger(temp, 2);
                if (num.compareTo(BigInteger.valueOf(k)) <= 0) {
                    ans.insert(0, '1');
                }
            }
        }
        return ans.length();
    }
}
